package com.itbour.web.action.video;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.tg.KeywordInfoDto;
import com.itbour.dto.video.YjxPriceInfoDto;
import com.itbour.service.video.VideoDesignInfoService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 视频设计模板一览
 * 1：列表查看视频设计模板列表
 * 2：发布视频
 * 3：取消发布视频
 * 4：删除视频设计模板
 * 5：按条件查询视频列表
 * @author LQ 
 */
public class GetVideoDesignInfoAction extends BaseAction {

	/**
	 */
	private static final long serialVersionUID = 1L;// 序列号
	private VideoDesignInfoService videoDesignInfoService;// 定义视频接口
	
	
	// 给视频添模板添加 表单
	public void addVideFormId() throws Exception{
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto) session.get("user");
		HttpServletRequest request = ServletActionContext.getRequest();
		String unItemId = request.getParameter("unItemId");
		String formId = request.getParameter("formId");
		if (user != null && user.getUser_id() != null) {
			Map<String, Object> parm = new HashMap<String, Object>();
			parm.put("uni_item_id", unItemId);
			parm.put("form_id", formId);
			parm.put("update_by", "kris:"+user.getUser_id());
			boolean b = videoDesignInfoService.addVideoFormId(parm);
			if (b){
				this.writeJSON("ok");
			} else {
				this.writeJSON("no");
			}
		} else {
			this.writeJSON("no");
		}
	}
	
	/**
	 * 分页获取视频设计信息
	 * 参数：page页数  rows行数
	 */
	private Map<String, Object> listVideoDis =new HashMap<String, Object>();// 视频内容列表
	private String findVip;// 视频vip状态
	private String findPubFlg;// 视频模板发布状态
	private String findTmplName;// 视频模板名称
	private String findTmplId;// 视频模板ID
	private String findItemId;// 视频关联商品ID
	private String findSort;// 0 默认 1按支付次数排序
	private String findDateStart;// 开始时间
	private String findDateEnd;// 结束时间
	public String getVideoDesignInfo(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null) {
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, Object> parm =new HashMap<String, Object>();
		String pageNo = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		if (StringUtils.isNotEmpty(findVip)){
			parm.put("vip",findVip);
		}
		if (StringUtils.isNotEmpty(findPubFlg)){
			parm.put("publish",findPubFlg);
		}
		if (StringUtils.isNotEmpty(findTmplName)){
			parm.put("design_name",findTmplName);
		}
		if (StringUtils.isNotEmpty(findTmplId)){
			parm.put("design_id",findTmplId);
		}
		if (StringUtils.isNotBlank(findItemId)){
			parm.put("item_id",findItemId);
		}
		if (StringUtils.isNotBlank(findDateStart)){
			parm.put("start_time",findDateStart);
		}
		if (StringUtils.isNotBlank(findDateEnd)){
			parm.put("end_time",findDateEnd);
		}
		if(StringUtils.isNotEmpty(findUserId)){
			parm.put("user_id",findUserId);// 检索指定人员作品
		}
		// 默认只检索自己的文档([管理员,模版操作员]除外)
		if(!user.isAdmin() && (user.getRole()&32)!=32) {
			parm.put("user_id", user.getUser_id());
		}
		if (StringUtils.isNotEmpty(findSort)){
			String ret ="";
			switch (Integer.parseInt(findSort)){
			case 1: ret = "x5.cnt DESC";break;
			}
			parm.put("sort", ret);
		} else {
			parm.put("sort", "vd.update_time desc");
		}
		Page  page = videoDesignInfoService.pageVideoDesignInfo(parm, 
				Integer.parseInt(pageNo), Integer.parseInt(pageSize));
		listVideoDis.put("total", page.getTotalCount());
		listVideoDis.put("rows",page.getResult());
		}
		return SUCCESS;
	}
	
	
	/**
	 * 修改视频设计模板内容
	 * 参数：publish 0取消发布 1发布视频
	 * 参数：del 1删除视频
	 * 参数：order 视频顺序
	 * 参数：vip 视频的vip类型
	 * 参数：price 视频价格
	 * @throws Exception 
	 */
	public void upVideoDesignInfo() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setCharacterEncoding("UTF-8");
		String ids = request.getParameter("ids");
		String publish = request.getParameter("publish");
		String del = request.getParameter("del");
		String order = request.getParameter("order");
		String vip = request.getParameter("vip");
		String price = request.getParameter("price");
		String theme_thumb = request.getParameter("theme_thumb");
		String desName = request.getParameter("desName");
		if (StringUtils.isNotBlank(ids)) {
			Map<String, Object> session = ActionContext.getContext().getSession();
		    UserInfoDto user = (UserInfoDto)session.get("user");
		    if (user!=null && user.getUser_id()!=null){
		    	List<Long> listId = StringUtils.stringToList2(ids);
		    	Map<String, Object> param = new HashMap<String, Object>();
		    	if (StringUtils.isNotEmpty(publish)){
		    		param.put("publish", publish);
		    		if (Integer.parseInt(publish) == 0){// 如果 publish ==0 取消发布 模板组的视频 统一删除
		    			String uniItemId = request.getParameter("uniItemId");// 统一模板ID
		    			Map<String, Object> tmp = new HashMap<String, Object>();
		    			tmp.put("update_by",user.getUser_name()+"##"+user.getUser_id());
		    			tmp.put("uni_item_id",uniItemId);
		    			videoDesignInfoService.delTmplSetRInfo(tmp);
		    		}
		    		
		    	}
		    	if (StringUtils.isNotEmpty(del)){
		    		if (user.isAdmin() || (user.getRole()&32)==32){// 管理员和模板操作员有权限删除
		    			param.put("del_flg", del);
		    		} else {
		    			this.writeJSON("no");
		    			return;
		    		}
		    	}
		    	if (StringUtils.isNotEmpty(order)){
		    		param.put("order", order);
		    	}
		    	if (StringUtils.isNotEmpty(vip)){
		    		param.put("vip", vip);
		    	}
		    	if (StringUtils.isNotEmpty(price)){
		    		param.put("price", price);
		    	}
		    	if (StringUtils.isNotEmpty(theme_thumb)){
		    		param.put("theme_thumb", theme_thumb);
		    	}
		    	if (StringUtils.isNotEmpty(desName)){
		    		param.put("design_name", desName);
		    	}
				param.put("ids", StringUtils.listToString2(listId));
				param.put("update_by", user.getUser_id()+user.getUser_name());
				boolean b = videoDesignInfoService.updVideoDesignInfo(param);
				if (b){
					this.writeJSON("ok");
				} else {
					this.writeJSON("ng");
				}
		    } else {
		    	this.writeJSON("ng");
		    }
		} else {
			this.writeJSON("ng");
		}
		
	}

	
	
	/* 	获取复审核 视频列表信息
	 */
	private Map<String, Object> LisAuditVideo =new HashMap<String, Object>();// 复审核列表
	private String findDesignId;// 
	private String findTmplState;// 模板状态
	private String findPub;// 视频模板发布状态
	private String findTmpName;// 视频模板发布名称
	public String getAuditVideoDesignInfo(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, Object> parm =new HashMap<String, Object>();
		String pageNo = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		if(StringUtils.isNotEmpty(findPub)){
			parm.put("publish",findPub);
		}
		if(StringUtils.isNotEmpty(findTmpName)){
			parm.put("design_name",findTmpName);
		}
		if(StringUtils.isNotEmpty(findDesignId)){
			parm.put("design_id",findDesignId);
		}
		if(StringUtils.isNotBlank(findTmplState)){
			parm.put("state",findTmplState);
		}
		Page  page = videoDesignInfoService.pageAuditVideoDesignInfo(parm, Integer.parseInt(pageNo), Integer.parseInt(pageSize));
		LisAuditVideo.put("total", page.getTotalCount());
		LisAuditVideo.put("rows",page.getResult());
		return SUCCESS;
	}
	
	/**
	 * 复审 视频模版信息
	 * 参数：视频模版ids
	 * @throws Exception 
	 */
	public void upVideoDesignState() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("ids");
		String state = request.getParameter("state");// state=32复审通过   state=41打回去重审
		String content = request.getParameter("chkCont");// 复审打回理由
		if (StringUtils.isNotBlank(ids) && StringUtils.isNotEmpty(state)) {
			Map<String, Object> session = ActionContext.getContext().getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	if(user!=null && user.getUser_id()!=null){
	    		List<Long> listId = StringUtils.stringToList2(ids);
		    	// 修改视频模板的state 审核状态
	    		Map<String, Object> param = new HashMap<String, Object>();
				param.put("state", state);
				param.put("ids", StringUtils.listToString2(listId));
				param.put("update_by", user.getUser_id());
				boolean b = videoDesignInfoService.updVideoDesignInfo(param);
				if (b){// 如果复审成功 则插入一条复审记录
					// 添加评审纪录
					Map<String, Object> insParam = new HashMap<String, Object>();
					insParam.put("design_id", StringUtils.listToString2(listId));// TODO
					insParam.put("state", state);
					insParam.put("kind",20);//10静态模版 20视频模版
					insParam.put("content", content);
					insParam.put("reviewer", user.getUser_id());
					insParam.put("update_by", user.getUser_id());
					videoDesignInfoService.insertReviewHis(insParam);// 插入复审核记录
					this.writeJSON("ok");
				} else{
					this.writeJSON("ng");
				}
	    	}
		} else{
			this.writeJSON("ng");
		}
	}
	
	/**获取用户视频 列表
	 */
	private Map<String, Object> userVideoLis =new HashMap<String, Object>();
	private String findDocId;// 文档ID
	private String findUserId;// 用户ID
	private String findMobile;// 手机号
	private String findState;// 购买状态
	private String findTestFlg;// 用户属性
	
	public String getUserVideoDoc(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, Object> parm =new HashMap<String, Object>();
		String pageNo = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		if(StringUtils.isNotEmpty(findDocId)){
			parm.put("doc_id",findDocId);
		}
		if(StringUtils.isNotEmpty(findUserId)){
			parm.put("user_id",findUserId);
		}
		if(StringUtils.isNotBlank(findMobile)){
			parm.put("mobile",findMobile);
		}
		if(StringUtils.isNotBlank(findState)){// 购买状态
			parm.put("state",findState);
		}
		if(StringUtils.isNotBlank(findDateStart)){// 开始时间
			parm.put("start_time",findDateStart);
		}
		if(StringUtils.isNotBlank(findDateEnd)){// 结束时间
			parm.put("end_time",findDateEnd);
		}
		
		if(StringUtils.isNotBlank(findTestFlg)){// 真实用户和测试用户
			parm.put("test_flg",findTestFlg);
		}
		String designId = request.getParameter("disId");
		Map<String, Object> result =new HashMap<String, Object>();
		if(StringUtils.isNotEmpty(designId)){
			parm.put("design_id",designId);
			Page  page = videoDesignInfoService.pageUserViderDoc(parm, Integer.parseInt(pageNo), Integer.parseInt(pageSize));
			result.put("total", page.getTotalCount());
			result.put("rows",page.getResult());
		}
		userVideoLis = result;
		return SUCCESS;
	}
	public String getFindState() {
		return findState;
	}
	public void setFindState(String findState) {
		this.findState = findState;
	}
	

	// 获取视频模板关键词
	private List<Object> keywordList;// 关键词集合
	private String keyDesignId;// 视频模板ID
	public String getKeywordLst(){
		if (keywordList == null){
			keywordList = new ArrayList<Object>();
		}
		keywordList.clear();// 清空
		
		Map<String, Object> param = new HashMap<String, Object>();
		if(keyDesignId!=null && Long.valueOf(keyDesignId)>0){
			param.put("design_id", keyDesignId);
		}
		List<KeywordInfoDto> kwList = videoDesignInfoService.selectKeywordInfo(param);
		if(kwList != null && kwList.size()>0){
			Integer tmpType = kwList.get(0).getType();
			List<KeywordInfoDto> subList = new ArrayList<KeywordInfoDto>();
			// 按type分组
			for(int i=0; i<kwList.size(); i++){
				KeywordInfoDto info = kwList.get(i);
				if(info.getType() != tmpType){
					keywordList.add(subList);
					subList = new ArrayList<KeywordInfoDto>();
					tmpType = info.getType();
				}
				subList.add(info);
				if(i==kwList.size()-1){
					keywordList.add(subList);
				}
			}
		}
		return SUCCESS;
	}
	
	// 获取视频设计模板套餐 kris
	private List<Object> viMeal;// 视频价格套餐
	private String uItemId;// 视频统一模板ID
	public String getVideoPriceMealInfo(){
		if (viMeal == null){
			viMeal = new ArrayList<Object>();
		}
		viMeal.clear();// 清空
		
		Map<String, Object> param = new HashMap<String, Object>();
		if(uItemId!=null && Long.valueOf(uItemId)>0){
			param.put("uni_item_id", uItemId); // 统一视频设计模板ID
		}
		List<YjxPriceInfoDto> priList = videoDesignInfoService.selectPriceInfo(param);
		if(priList != null && priList.size()>0){
//			Long tmpType = priList.get(0).getPrice_type();
//			List<YjxPriceInfoDto> subList = new ArrayList<YjxPriceInfoDto>();// 判断放到一个数组里面
			List<YjxPriceInfoDto> subList = new ArrayList<YjxPriceInfoDto>();// 判断放到一个数组里面
			List<YjxPriceInfoDto> subList1 = new ArrayList<YjxPriceInfoDto>();// 判断放到一个数组里面
			List<YjxPriceInfoDto> subList2 = new ArrayList<YjxPriceInfoDto>();// 判断放到一个数组里面
			List<YjxPriceInfoDto> subList3 = new ArrayList<YjxPriceInfoDto>();// 判断放到一个数组里面
			// 按type分组
			for(int i=0; i<priList.size(); i++){ 
				Long tmpType = priList.get(i).getPrice_type();// 类型
				YjxPriceInfoDto info = priList.get(i);
				
				//价格类型: 2[去水印订单],4[秒杀订单],8[vip订单],16[待定]
				switch(Integer.parseInt(tmpType.toString())){
				case 2 : info.setType_name("去水印订单");subList.add(info);break; 
				case 4 : info.setType_name("秒杀订单");;subList1.add(info);break; 
				case 8 : info.setType_name("vip订单");subList2.add(info);break; 
				case 16 :info.setType_name("充值订单");subList3.add(info);break; 
				}
			}
			viMeal.add(subList);
			viMeal.add(subList1);
			viMeal.add(subList2);
			viMeal.add(subList3);
		}
		return SUCCESS;
	}
	
	// 添加视频套餐信息 kris
	public void addPriceRInfo() throws Exception{
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto) session.get("user");
		HttpServletRequest request = ServletActionContext.getRequest();
		String uitemId = request.getParameter("uItemId");
		String price_id = request.getParameter("price_id");
		if (user != null && user.getUser_id() != null && uitemId !=null) {
			Map<String, Object> parm = new HashMap<String, Object>();
			String[] ids = StringUtils.str2Array(price_id);
			parm.put("update_by",user.getUser_id()+"|"+user.getNickname());
			parm.put("uni_item_id",uitemId);
			boolean b = false;
			b = videoDesignInfoService.delPriceRInfo(parm);
			if (b){ // 先删除 判断是否删除成功
				for (String id : ids) {
					parm.put("price_id", id);
					b = videoDesignInfoService.addPriceRInfo(parm);
				}
				if (b){
					this.writeJSON("ok");
				} else {
					this.writeJSON("no");
				}
			} else {
			   this.writeJSON("no");
			}
		} else {
			this.writeJSON("no");
		}
	}
	
	//################################ 生成Set和Get方法  #############################################
	public VideoDesignInfoService getVideoDesignInfoService() {
		return videoDesignInfoService;
	}
	public List<Object> getViMeal() {
		return viMeal;
	}
	public void setViMeal(List<Object> viMeal) {
		this.viMeal = viMeal;
	}
	public String getuItemId() {
		return uItemId;
	}
	public void setuItemId(String uItemId) {
		this.uItemId = uItemId;
	}


	public void setVideoDesignInfoService(
			VideoDesignInfoService videoDesignInfoService) {
		this.videoDesignInfoService = videoDesignInfoService;
	}
	public Map<String, Object> getListVideoDis() {
		return listVideoDis;
	}
	public void setListVideoDis(Map<String, Object> listVideoDis) {
		this.listVideoDis = listVideoDis;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public String getFindVip() {
		return findVip;
	}
	public void setFindVip(String findVip) {
		this.findVip = findVip;
	}
	public String getFindPubFlg() {
		return findPubFlg;
	}
	public void setFindPubFlg(String findPubFlg) {
		this.findPubFlg = findPubFlg;
	}
	public String getFindTmplName() {
		return findTmplName;
	}
	public void setFindTmplName(String findTmplName) {
		this.findTmplName = findTmplName;
	}
	public String getFindTmplId() {
		return findTmplId;
	}
	public void setFindTmplId(String findTmplId) {
		this.findTmplId = findTmplId;
	}
	public String getFindItemId() {
		return findItemId;
	}
	public void setFindItemId(String findItemId) {
		this.findItemId = findItemId;
	}
	public Map<String, Object> getLisAuditVideo() {
		return LisAuditVideo;
	}
	public void setLisAuditVideo(Map<String, Object> lisAuditVideo) {
		LisAuditVideo = lisAuditVideo;
	}
	public String getFindDesignId() {
		return findDesignId;
	}
	public void setFindDesignId(String findDesignId) {
		this.findDesignId = findDesignId;
	}
	public String getFindTmplState() {
		return findTmplState;
	}
	public void setFindTmplState(String findTmplState) {
		this.findTmplState = findTmplState;
	}
	public String getFindPub() {
		return findPub;
	}
	public void setFindPub(String findPub) {
		this.findPub = findPub;
	}
	public String getFindTmpName() {
		return findTmpName;
	}
	public void setFindTmpName(String findTmpName) {
		this.findTmpName = findTmpName;
	}
	public Map<String, Object> getUserVideoLis() {
		return userVideoLis;
	}
	public void setUserVideoLis(Map<String, Object> userVideoLis) {
		this.userVideoLis = userVideoLis;
	}
	public String getFindDocId() {
		return findDocId;
	}
	public void setFindDocId(String findDocId) {
		this.findDocId = findDocId;
	}
	public String getFindUserId() {
		return findUserId;
	}
	public void setFindUserId(String findUserId) {
		this.findUserId = findUserId;
	}
	public String getFindMobile() {
		return findMobile;
	}
	public void setFindMobile(String findMobile) {
		this.findMobile = findMobile;
	}
	public List<Object> getKeywordList() {
		return keywordList;
	}
	public void setKeywordList(List<Object> keywordList) {
		this.keywordList = keywordList;
	}
	public String getKeyDesignId() {
		return keyDesignId;
	}
	public void setKeyDesignId(String keyDesignId) {
		this.keyDesignId = keyDesignId;
	}
	public String getFindSort() {
		return findSort;
	}
	public void setFindSort(String findSort) {
		this.findSort = findSort;
	}

	public String getFindTestFlg() {
		return findTestFlg;
	}
	public void setFindTestFlg(String findTestFlg) {
		this.findTestFlg = findTestFlg;
	}


	public String getFindDateStart() {
		return findDateStart;
	}
	public void setFindDateStart(String findDateStart) {
		this.findDateStart = findDateStart;
	}
	public String getFindDateEnd() {
		return findDateEnd;
	}
	public void setFindDateEnd(String findDateEnd) {
		this.findDateEnd = findDateEnd;
	}
	
}
